$(function() {
    var emptyImgTpl = Handlebars.compile($('#tmpl-gic_empty_view_tpl').html());
    var gic_tab,gic_draft_list,gic_draft_list_item;
    var gic_audit_list,gic_audit_list_item;
    var gic_online_list,gic_reject_list;
    !function(){
        var itemTpl;
        gic_tab = sjApp.extend(superclass,{
            defaultActive : 'detail',
            createItems : function () {
            },
            init : function () {
                this.dataArray = [{
                    id: 'draft',
                    title: '草稿',
                    href: global_json.gic_draft_url,
                    cmp : gic_draft_list
                }, {
                    id: 'audit',
                    title: '审核中',
                    href: global_json.gic_audit_url,
                    cmp : gic_audit_list
                }, {
                    id: 'online',
                    title: '已上线',
                    href: global_json.gic_online_url,
                    cmp : gic_online_list
                }, {
                    id: 'reject',
                    title: '已驳回',
                    href: global_json.gic_reject_url,
                    cmp : gic_reject_list
                }];
                this.El = $('<div class="tabs_wrapper fix_width_on_modal gicTab"></div>');

                this.El.append(itemTpl({
                    data : this.dataArray
                }));
                this.dataJson = {};
                for (var i = 0,obj;obj =this.dataArray[i]; i++) {
                    this.dataJson[obj.href] = obj;
                }
                return this.El;
            }, initEvent: function () {
                var me = this;
                sjApp.load_comments_page.setHandler(function (url) {
                    url = url.replace(/.*\?/g,'?');
                    var idIndex = 'gic_tab_';
                    var dJson = me.dataJson[url];
                    if (!dJson) {
                        dJson = me.dataArray[0];
                    }
                    if (dJson) {
                        var curA =$('#'+idIndex+dJson.id);
                        curA.parent().addClass('active');
                        if (dJson.id == 'about') {
                            var iframObj = $('<iframe id="mainiframe" src="'+cur_contest.refer.replace(/http:\/\/(www\.|)500px.me/g,'')+'" name="mainiframe" width="'+(sjApp.ismobile?$(window).width():'100%')+'" style="height:800px;" frameborder="0" scrolling="no" marginwidth="0" marginheight="0" />');
//							iframObj.load(function(){
//								var mainheight = $(this).contents().find("body").height()+30;
//								$(this).height(mainheight);
//							});
                            $('.profile_body').html(iframObj);
                        }else{
                            if(dJson.cmp){
                                $('.profile_body').html(new dJson.cmp().El);
                            }else{
                                $.ajax({
                                    type: "get",
                                    dataType:'json',
                                    data : {
                                        page : 1,
                                        size : 100,
                                        type : 'json'
                                    },
                                    url: dJson.href,
                                    success: function(data, textStatus){
                                        alert(data)
                                    },
                                    error: function(){
                                        //请求出错处理
                                        // alert("error");
                                    }
                                });
                            }
                        }
                    }
                });
                this.El.find('a').click(function (e) {
                    if (!e.isTrigger) {
                        sjApp.cookieRemove(window.location.pathname+window.location.search);
                    }
                    me.El.find('.active').removeClass('active');
                    $(this).parent().addClass('active');
                    sjApp.load_comments_page.init.apply(this,arguments);
                });
                $(window).load(function () {
                    sjApp.load_comments_page.init.apply(this,arguments);
                });
                window.onpopstate = sjApp.onpopstate;
            }
        });
        gic_tab.init = function () {
            itemTpl = Handlebars.compile($('#tmpl-gic_user_tab').html());
        };
    }();

    !function(){
        var itemTpl;
        gic_draft_list = sjApp.extend(superclass,{
            getEmptyEl : function () {
                return $(emptyImgTpl({
                    title : '您还没有作品！',
                    desc : '期待您的佳作，点此<a href="/community/upload_entry.html">上传</a>'
                }));
            },
            getListUrl : function(){
                return global_json.gic_draft_list;
            },
            getItem : function(obj){
                return new gic_draft_list_item({
                    data : obj
                }).El
            },
            getReqData : function(index){
                return {
                    page : index,
                    size : 2,
                    type : 'json'
                }
            },
            init : function () {
                this.El = $('<div class="sets_body infinite_scroll_container single_page active pagination_finished"></div>');
                return this.El;
            }, createItems: function () {
                var me = this;
                var index = 1;
                var loadCmp = new loading_cmp({
                    parentEl : this.El
                });
                var loadData = function () {
                    $.ajax({
                        type : "GET",
                        context : this,
                        // 提交的网址
                        url : this.getListUrl({
                            index : index
                        }),
                        data : this.getReqData(index),
                        // 提交的数据
                        dataType : "json",
                        success : function(data) {
                            loadCmp.del();
                            var g = this.El;
                            if (data instanceof Object && (data.items instanceof Array||data.data instanceof Array)) {
                                data = data.items||data.data;
                            }
                            if (data && data.length) {
                                for (var i = 0, obj; obj = data[i]; i++) {
                                    if (me.validate instanceof Function && !me.validate(obj)) {
                                        continue;
                                    }
                                    g.append(this.getItem(obj));
                                }

                                loadCmp.createLazy(function () {
                                    index++;
                                    loadData.call(me);
                                },index);

                                $(window).scroll();
                            }else if(index == 1){
                                var emptyEl = me.getEmptyEl();
                                if (emptyEl) {
                                    g.append(emptyEl);
                                }
                            }
                        },
                        error : function(e) {
                            sjApp.errorMsg(e);
                        }
                    });
                }
                loadData.call(this);
            },initEvent : function(){
                this.El.delegate('.photo__top_buttons__close_button','click',function(){
                    var a = confirm("是否删除该草稿？");
                    if(a){
                        var _this = $(this);
                        var draftId = _this.data('id');
                        $.ajax({
                            // 提交数据的类型 POST GET
                            type : "POST",
                            // 提交的网址
                            url : '/community/gic/delete',
                            // 提交的数据
                            data : {
                                draftId : draftId
                            },
                            // 返回数据的格式
                            dataType : "text",// "xml", "html", "script", "json",
                            // "jsonp", "text".
                            // 在请求之前调用的函数
                            // beforeSend:function(){$("#msg").html("logining");},
                            // 成功返回之后调用的函数
                            success : function(data) {
                                if(data){
                                    _this.parents('.set_item.px_card').remove();
                                    sjApp.showMsg({
                                        html : '删除草稿成功',
                                        cls : 'success'
                                    })
                                }
                            },
                            // 调用执行后调用的函数
                            // complete: function(XMLHttpRequest, textStatus){
                            // alert(XMLHttpRequest.responseText);
                            // alert(textStatus);
                            // //HideLoading();
                            // },
                            // 调用出错执行的函数
                            error : function(e) {
                                sjApp.showMsg({
                                    html : '删除失败！',
                                    cls : 'error'
                                })
                                // 请求出错处理
                            }
                        });
                    }
                });
            }
        });
    }();

    !function(){
        var itemTpl;
        gic_draft_list_item = sjApp.extend(superclass,{
            init : function () {
                this.El = $('<div class="set_item px_card medium no_badge no_avatar"></div>');
                this.El.append(itemTpl(this.data));
                this.El.find("div[data-original]").lazyload({
                    load : function() {
                    },
                    effect : "fadeIn"
                });
                return this.El;
            }, createItems: function () {
            }
        });
        gic_draft_list_item.init = function () {
            itemTpl = Handlebars.compile($('#tmpl-gic_draft_list').html())
        };
    }();


    /*审核中begin*/
    !function(){
        var itemTpl;
        gic_audit_list = sjApp.extend(gic_draft_list,{
            initEvent : function(){},
            getReqData : function(index){
                return {}
            },
            dataState : 1,
            getListUrl : function(ops){
                var u = global_json.gic_audit_list+'?url='+escape('http://api.vcg.com/v1/photographs?per-page=20&page='+ops.index+'&uid='+SJ_UT.id+'&status='+this.dataState);
                return u;
            },
            getItem : function(obj){
                return new gic_audit_list_item({
                    data : obj
                }).El
            },
            init : function () {
                this.El = $('<div class="sets_body infinite_scroll_container single_page active pagination_finished"></div>');
                return this.El;
            }
        });
        gic_audit_list.init = function () {
        };
    }();

    !function(){
        var itemTpl;
        gic_audit_list_item = sjApp.extend(superclass,{
            init : function () {
                this.El = $('<div class="set_item px_card medium no_badge no_avatar gic_audit"></div>');
                this.El.append(itemTpl(this.data));
                this.El.find("div[data-original]").lazyload({
                    load : function() {
                    },
                    effect : "fadeIn"
                });
                return this.El;
            }, createItems: function () {
            }
        });
        gic_audit_list_item.init = function () {
            itemTpl = Handlebars.compile($('#tmpl-gic_audit_list').html())
        };
    }();
    /*审核中end*/
    //已上线
    !function(){
        gic_online_list = sjApp.extend(gic_audit_list,{
            dataState : 2
        });
    }();
    //已驳回
    !function(){
        gic_reject_list = sjApp.extend(gic_audit_list,{
            dataState : 3
        });
    }();
    $('.profile_body').before(new gic_tab().El);
});